﻿using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Amoo.Code.Common
{
    public class Util
    {
        public static class Config
        {
            public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings["conn"].ConnectionString; } }
            public static string DomainEmail { get { return ConfigurationManager.AppSettings["domainEmail"]; } }
            public static string GetValueByKey(string key)
            {
                if (ConfigurationManager.AppSettings[key] != null)
                {
                    return ConfigurationManager.AppSettings[key];
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        public static class Form
        {
            public static Control ValidateWebForm(Panel groupForm)
            {
                return ValidateWebForm(groupForm, null);
            }
            public static Control ValidateWebForm(Panel groupForm, IList<Control> exceptionControls)
            {
                foreach (Control control in groupForm.Controls)
                {
                    if (exceptionControls!=null &&exceptionControls.Contains(control))
                        break;

                    if (control is TextBox)
                    {
                        return (control as TextBox).Text == "" ? control : null;
                    }
                    else if (control is DropDownList)
                    {
                        return (control as DropDownList).SelectedIndex < 0 ? control : null;
                    }
                    else if (control is ListBox)
                    {
                        return (control as DropDownList).SelectedIndex < 0 ? control : null;
                    }
                    else if (control is CheckBox)
                    {
                        return (control as CheckBox).Checked == false ? control : null;
                    }
                    else
                    {
                        return null;
                    }

                }
                return null;
            }
            public static void ClearWebForm(Panel groupForm)
            {
                foreach (Control control in groupForm.Controls)
                {
                    if (control is TextBox)
                    {
                        (control as TextBox).Text = "";
                    }
                    else if (control is DropDownList)
                    {
                        (control as DropDownList).SelectedIndex = 0;
                    }
                    else if (control is ListBox)
                    {
                        (control as DropDownList).SelectedIndex = 0;
                    }
                    else if (control is CheckBox)
                    {
                        (control as CheckBox).Checked = false;
                    }
                }
            }
        }
    }
}
